// pages/petCardInformation/petCardInformation.js
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: null,
    age: "",
    data: [],
    page: 1,
    isOneself: true,
    isFocusState: true, //true 代表未关注  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    var item = JSON.parse(options.item)
    console.log(item)
    var year = 1000 * 60 * 60 * 24 * 365;
    var now = new Date();
    var age = parseInt((now - item.birthday) / year);
    wx.showLoading({
      title: '加载中...',
    })
    setTimeout(() => {
      that.setData({
        item: item,
        age: age,
        isOneself: item.wxgzUserId == getApp().globalData.userInfos.id ? true : false,
      })
      that.getPetPetshowList();
      if (item.wxgzUserId != getApp().globalData.userInfos.id) {
        that.getFocusState();
      }
    }, 1000)




    //console.log(item)
  },
  /**
   * 获取个人发布的动态
   */
  getPetPetshowList: function(e) {
    console.log(that.data.page)
    /**
     *  获取个人 / 他人动态列表，如果token不为空表示获取自己的，如果userId不为空表示获取别人的
     */
    wx.request({
      url: getApp().globalData.urls + '/wxgz/petshow/getPetPetshowList',
      method: "GET",
      data: {
        token: wx.getStorageSync("token"),
        petId: that.data.item.id,
        page: that.data.page,
        rows: 20,
      },
      header: {
        'content-type': 'application/json' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function(res) {
        wx.hideLoading()
        console.log("aaa", res.data)
        if (res.data.code == 200) {
          var list = that.data.data;
          var datas = res.data.data;
          for (var i = 0; i < datas.length; i++) {
            datas[i].imageUrl = datas[i].imageUrl.split(',');
            list.push(datas[i])
          }
          that.setData({
            data: list
          })
        } else {
          wx.showToast({
            title: res.data.msg,
          })
        }
      },
      fail: function(e) {
        wx.hideLoading()
      }
    })


  },
  /**
   * 是否已关注
   */
  getFocusState: function(e) {
    wx.request({
      url: getApp().globalData.urls + '/wxgz/petManage/getFocusState',
      method: "GET",
      data: {
        token: wx.getStorageSync("token"),
        petId: that.data.item.id,
      },
      header: {
        'content-type': 'application/json' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function(res) {

        console.log("acccccc", res.data)
        if (res.data.code == 200) {
          that.setData({
            isFocusState: res.data.data
          })
        } else {

        }
      },
      fail: function(e) {

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (that.data.item != null){

    that.getPetInfo()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    that.data.page = 1;
    that.data.data = [];
    that.getPetPetshowList();
    // 小程序提供的api，通知页面停止下拉刷新效果
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '加载更多',
    })
    that.data.page = that.data.page + 1;
    that.getPetPetshowList();
    // 小程序提供的api，通知页面停止下拉刷新效果
    wx.stopPullDownRefresh();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  handleTap: function(e) {
    //console.log(e.currentTarget.dataset.data)
    var data = e.currentTarget.dataset.data
    wx.navigateTo({
      url: '../publishContent/publishContent?id=' + data.id + "&sends=" + data.sends + "&zans=" + data.zans + "&zanState=" + data.zanState,
    })
  },


  /**
   * 关注和取消关注或编辑
   */
  saveFocus: function(e) {
    if (that.data.isOneself) {
      //自己的宠卡
      wx.navigateTo({
        url: '../petCardAdd/petCardAdd?isSee=2&' + "item=" + JSON.stringify(that.data.item),
      })



    } else {
      //别人的宠卡
      wx.showLoading({
        title: '',
      })
      wx.request({
        url: getApp().globalData.urls + '/wxgz/petManage/saveFocus',

        data: {
          token: wx.getStorageSync("token"),
          petId: that.data.item.id,
          state: that.data.isFocusState ? 1 : 2
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
        },
        success: function(res) {
          wx.hideLoading()
          console.log("acccccc", res.data)
          if (res.data.code == 200) {
            that.setData({
              isFocusState: !that.data.isFocusState
            })
            wx.showToast({
              title: that.data.isFocusState ? "取消关注" : "已关注",
            })
          } else {

          }
        },
        fail: function(e) {
          wx.hideLoading()
        }
      })

    }
  },
  getPetInfo: function (e) {
    wx.request({
      url: getApp().globalData.urls + '/wxgz/pet/getPetInfo',
      method: "GET",
      data: {
        petId: that.data.item.id
      },
      header: {
        'content-type': 'application/json' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
        if (res.data.code == 200) {
          var item = res.data.data;
          var year = 1000 * 60 * 60 * 24 * 365;
          var now = new Date();
          var age = parseInt((now - item.birthday) / year);
            that.setData({
              item: item,
              age: age,
              isOneself: item.wxgzUserId == getApp().globalData.userInfos.id ? true : false,
            })
        }
      }


    })
  },

})